package visaoInterface;

import dao.DaoTipoVeiculo;
import java.util.Iterator;
import java.util.Vector;
import dao.DaoVeiculo;
import dao.DaoCliente;
import dao.DaoEndereco;
import javax.swing.JOptionPane;
import modelo.dominio.*;
import javax.swing.JOptionPane;
import modelo.dominio.TipoVeiculo;

public class ChamaVeiculo extends javax.swing.JDialog {

    /** Creates new form ChamaVeiculo */
    public ChamaVeiculo(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        DaoTipoVeiculo dtv = new DaoTipoVeiculo();
        Vector<TipoVeiculo> resultado = dtv.buscarTodos();
        for (Iterator<TipoVeiculo> iterator = resultado.iterator(); iterator.hasNext();) {  
         TipoVeiculo tv = iterator.next();  
         //System.out.println("Pessoa Encontrada: " + tv.getTipoVeiculo());  
         this.jCBTipoVeiculo.addItem(tv.getTipoVeiculo());
      }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLPlaca = new javax.swing.JLabel();
        jLMarca = new javax.swing.JLabel();
        jLModelo = new javax.swing.JLabel();
        jLCor = new javax.swing.JLabel();
        jLVTipoVeiculo = new javax.swing.JLabel();
        jLOpcionais = new javax.swing.JLabel();
        jLObs = new javax.swing.JLabel();
        jTFModelo = new javax.swing.JTextField();
        jTFMarca = new javax.swing.JTextField();
        jCBCor = new javax.swing.JComboBox();
        jCBTipoVeiculo = new javax.swing.JComboBox();
        jCBAr = new javax.swing.JCheckBox();
        jCBDirecao = new javax.swing.JCheckBox();
        jCBAbs = new javax.swing.JCheckBox();
        jCBAirbag = new javax.swing.JCheckBox();
        jCBTrioEletrico = new javax.swing.JCheckBox();
        jCB = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTPObs = new javax.swing.JTextPane();
        jBVCancelar = new javax.swing.JButton();
        jBVSalvar = new javax.swing.JButton();
        jCBBlindado = new javax.swing.JCheckBox();
        jCBAutomatico = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLKmAtual = new javax.swing.JLabel();
        jTFKmAtual = new javax.swing.JTextField();
        jTFPlaca = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro de Veículos"));

        jLPlaca.setText("Placa*");

        jLMarca.setText("Marca*");

        jLModelo.setText("Modelo*");

        jLCor.setText("Cor");

        jLVTipoVeiculo.setText("Tipo");

        jLOpcionais.setText("Opcionais");

        jLObs.setText("Obs.");

        jCBCor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cinza", "Preto", "Branco", "Vermelho", "Laranja", "Amarelo", "Verde", "Azul", "Roxo", "Azul Claro", "Verde Claro", "Rosa" }));

        jCBAr.setText("Ar");

        jCBDirecao.setText("Direção");

        jCBAbs.setText("ABS");

        jCBAirbag.setText("Airbag");

        jCBTrioEletrico.setText("Trio Elétrico");

        jCB.setText("DVD");

        jScrollPane1.setViewportView(jTPObs);

        jBVCancelar.setText("Cancelar");
        jBVCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBVCancelarActionPerformed(evt);
            }
        });

        jBVSalvar.setText("Salvar");
        jBVSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBVSalvarActionPerformed(evt);
            }
        });

        jCBBlindado.setText("Blindado");
        jCBBlindado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBBlindadoActionPerformed(evt);
            }
        });

        jCBAutomatico.setText("Automático");

        jLabel1.setText("* Campos Obrigatórios");

        jButton1.setText("Limpar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLKmAtual.setText("Km Atual");

        try {
            jTFPlaca.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("UUU-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLCor)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLMarca)
                                    .addComponent(jLPlaca))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTFMarca, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                                    .addComponent(jCBCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTFPlaca))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLKmAtual)
                                .addGap(18, 18, 18)
                                .addComponent(jTFKmAtual, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLModelo)
                                .addGap(18, 18, 18)
                                .addComponent(jTFModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLVTipoVeiculo)
                                .addGap(18, 18, 18)
                                .addComponent(jCBTipoVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLObs)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jBVSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(jBVCancelar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLOpcionais)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCBAirbag)
                            .addComponent(jCBAr))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jCBTrioEletrico)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCB))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jCBDirecao)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCBAbs)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCBAutomatico)
                            .addComponent(jCBBlindado))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPlaca)
                    .addComponent(jLModelo)
                    .addComponent(jTFModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLMarca)
                    .addComponent(jTFMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLKmAtual)
                    .addComponent(jTFKmAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLCor)
                    .addComponent(jCBCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLVTipoVeiculo)
                    .addComponent(jCBTipoVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLOpcionais)
                    .addComponent(jCBAr)
                    .addComponent(jCBDirecao)
                    .addComponent(jCBBlindado)
                    .addComponent(jCBAbs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBAirbag)
                    .addComponent(jCBTrioEletrico)
                    .addComponent(jCBAutomatico)
                    .addComponent(jCB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLObs)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBVCancelar)
                        .addComponent(jBVSalvar)
                        .addComponent(jButton1))
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCBBlindadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBBlindadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCBBlindadoActionPerformed

    private void jBVCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBVCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBVCancelarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //limpa os dados das caixas de texto.
        jTFPlaca.setText("");
        jTFMarca.setText("");
        jTFModelo.setText("");
        jTPObs.setText("");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jBVSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBVSalvarActionPerformed
        //Mostra na tabela os veiculos na tabela
        if(jTFPlaca.getText().equals("") || jTFMarca.getText().equals("") || jTFModelo.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencher os campos obrigatórios.");
        }else{
        int codTipoVeiculo = 0;
        Veiculo v = new Veiculo();
        DaoVeiculo dv = new DaoVeiculo();
        DaoTipoVeiculo dtv = new DaoTipoVeiculo();
        v.setPlaca(jTFPlaca.getText());
        v.setMarca(jTFMarca.getText());
        v.setModelo(jTFModelo.getText());
        v.setDisponivel(true);
        v.setKmAtual(Double.parseDouble(jTFKmAtual.getText()));
        v.setCor(jCBCor.getSelectedItem().toString());
        v.setTipoVeiculo(jCBTipoVeiculo.getSelectedItem().toString());
        Vector<TipoVeiculo> resultado = dtv.buscarTodos();
          for (Iterator<TipoVeiculo> iterator = resultado.iterator(); iterator.hasNext();) {  
            TipoVeiculo tv = iterator.next();
            if  (tv.getTipoVeiculo().equals(jCBTipoVeiculo.getSelectedItem().toString())){
                codTipoVeiculo = tv.getCod();
            }
          }
        dv.insere(v, codTipoVeiculo);
        jTPObs.setText("");
        jTFPlaca.setText("");
        jTFMarca.setText("");
        jTFModelo.setText("");
        jTFKmAtual.setText("");
        JOptionPane.showMessageDialog(null, "O Veiculo foi cadastrado com sucesso.");
        }
    }//GEN-LAST:event_jBVSalvarActionPerformed


    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChamaVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChamaVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChamaVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChamaVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChamaVeiculo dialog = new ChamaVeiculo(new javax.swing.JDialog(), true);
                
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBVCancelar;
    private javax.swing.JButton jBVSalvar;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCB;
    private javax.swing.JCheckBox jCBAbs;
    private javax.swing.JCheckBox jCBAirbag;
    private javax.swing.JCheckBox jCBAr;
    private javax.swing.JCheckBox jCBAutomatico;
    private javax.swing.JCheckBox jCBBlindado;
    private javax.swing.JComboBox jCBCor;
    private javax.swing.JCheckBox jCBDirecao;
    private javax.swing.JComboBox jCBTipoVeiculo;
    private javax.swing.JCheckBox jCBTrioEletrico;
    private javax.swing.JLabel jLCor;
    private javax.swing.JLabel jLKmAtual;
    private javax.swing.JLabel jLMarca;
    private javax.swing.JLabel jLModelo;
    private javax.swing.JLabel jLObs;
    private javax.swing.JLabel jLOpcionais;
    private javax.swing.JLabel jLPlaca;
    private javax.swing.JLabel jLVTipoVeiculo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTFKmAtual;
    private javax.swing.JTextField jTFMarca;
    private javax.swing.JTextField jTFModelo;
    private javax.swing.JFormattedTextField jTFPlaca;
    private javax.swing.JTextPane jTPObs;
    // End of variables declaration//GEN-END:variables
}
